package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public
    Long save(User user) {
        //执行
        userMapper.insert(user);

        //mybatis-plus自动返回主键，封装到user中
        return  user.getId();
    }

    @Override
    public User findByPhone(String phone) {

        //追加条件where phone = #{phone}
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("phone",phone);

        //执行条件查询
        User user = userMapper.selectOne(userQueryWrapper);
        return user;
    }

    public User findByUserId(Long userId) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("id", userId);
        User user =userMapper.selectOne(wrapper);
        return user;
    }

}
